﻿using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using VM.Common.Result;
using VM.Entity.EntityDto.TitleDto;
using VM.Entity.EntityMap.Condition;
using VM.Entity.EntityMap.Files;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Title;
using VM.Entity.EntityMap.Topic;
using VM.IDAL;

namespace VM.DAL
{
    public class TitleDAL :  BaseDAL,ITitleDAL
    {
        protected ISqlSugarClient _Client { get; set; }
        private readonly IMapper _IMapper;
        public TitleDAL(ISqlSugarClient client, IMapper iMapper) : base(client)
        {
            _Client = client;
            _IMapper= iMapper;
        }
 
        public PagingData<titleListDto> getTopicTypeListConditon<T>(List<Condition> whereObj, int pageSize, int pageIndex, Expression<Func<T, object>> funcOrderby, bool isAsc = true) where T : class
        {

            var a = _Client.Queryable<sys_topic>().ToList();
            var listRes = _Client.Queryable<sys_titleParent>()

                            .Includes(a => a.main)
                            .Includes(a => a.files)
                            .Includes(a => a.topic, t => t.topicInfo)
                            ;
            
            //return null;
            PagingData<sys_titleParent> result = new PagingData<sys_titleParent>()
            {
                dataList = listRes.ToPageList(pageIndex, pageSize),
                pageIndex = pageIndex,
                pageSize = pageSize,
                recordCount = listRes.Count(),
            };

            PagingData<titleListDto> resend = _IMapper.Map<PagingData<sys_titleParent>, PagingData<titleListDto>>(result);

           return resend;
        }

        public  titleDetailDto getTitleDetailById(int id)
        {

            List<sys_titleParent> Res = _Client.Queryable<sys_titleParent>()

                            .Includes(a => a.main)
                            .Includes(a => a.files)
                            .Includes(a => a.topic, t => t.topicInfo)
                            .Where(a=>a.id==id)
                            .ToList();
            titleDetailDto result = null;
            if (Res.Count > 0) {
                sys_titleParent EntityRes = Res[0];
                result = _IMapper.Map<sys_titleParent, titleDetailDto>(EntityRes);

            }
            

            

            return result;

        }
    }
}
